package cn.yeyiu.mangaviewer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.bean.MangaEP;
import com.bumptech.glide.Glide;

import java.util.Locale;

public class MangaViewAdapter extends RecyclerView.Adapter<MangaViewAdapter.ViewHolder> {
    private final Context mContext;
    private final MangaEP mangaEp;

    public MangaViewAdapter(Context context, MangaEP mangaEp) {
        this.mContext = context;
        this.mangaEp = mangaEp;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(this.mContext).inflate(R.layout.item_manga_img, viewGroup, false));
    }

    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.imgId.setText(String.format(Locale.CHINA, "%03d", i+1));
        Glide.with(this.mContext)
                .load(mangaEp.picList.get(i))
                .placeholder(R.drawable.item_wait_load_bg)
                .into(viewHolder.img);
    }

    @Override
    public int getItemCount() {
        if(this.mangaEp.picList==null){
            return 0;
        }
        return this.mangaEp.picList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;
        public TextView imgId;

        public ViewHolder(View view) {
            super(view);
            this.img = view.findViewById(R.id.manga_img);
            this.imgId = view.findViewById(R.id.manga_img_id);
        }
    }
}
